package cn.com.xjnt.vas.manage.domain.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.google.common.collect.Lists;

/**
 * The persistent class for the sys_organization database table.
 * 
 */
@Entity
@Table(name = "sys_organization")
@NamedQuery(name = "Organization.findAll", query = "SELECT o FROM Organization o")
public class Organization extends com.frameworks.core.entity.IdEntity {
	private static final long serialVersionUID = 1L;

	@Column(nullable = false, length = 64)
	private String name;

	@Column(nullable = false)
	private int level;

	@Column(nullable = false)
	private int priority;

	@Column(length = 256)
	private String description;

	@Temporal(TemporalType.TIMESTAMP)
	private Date createDate;

	@Temporal(TemporalType.TIMESTAMP)
	private Date modifyDate;

	@ManyToOne
	@JoinColumn(name = "parent_id")
	private Organization parent;

	@OneToMany(mappedBy = "parent", cascade = { CascadeType.REMOVE }, orphanRemoval = true)
	private List<Organization> children = Lists.newArrayList();

	@OneToMany(mappedBy = "organization", cascade = { CascadeType.PERSIST, CascadeType.REMOVE }, orphanRemoval = true)
	private List<OrganizationRole> organizationRoles = Lists.newArrayList();

	public Organization() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public Organization getParent() {
		return parent;
	}

	public void setParent(Organization parent) {
		this.parent = parent;
	}

	public List<Organization> getChildren() {
		return children;
	}

	public void setChildren(List<Organization> children) {
		this.children = children;
	}
	
	public Organization addChildren(Organization children){
		children.setParent(this);
		this.getChildren().add(children);
		return children;
	}
	
	public Organization removeChildren(Organization children){
		children.setParent(null);
		this.getChildren().remove(children);
		return children;
	}

	public List<OrganizationRole> getOrganizationRoles() {
		return organizationRoles;
	}

	public void setOrganizationRoles(List<OrganizationRole> organizationRoles) {
		this.organizationRoles = organizationRoles;
	}
	
	public OrganizationRole addRole(OrganizationRole organizationRole){
		organizationRole.setOrganization(this);
		this.getOrganizationRoles().add(organizationRole);
		return organizationRole;
	}
	
	public OrganizationRole removeRole(OrganizationRole organizationRole){
		organizationRole.setOrganization(null);
		this.getOrganizationRoles().remove(organizationRole);
		return organizationRole;
	}

	@PrePersist
	public void onCreate() {
		this.createDate = this.modifyDate = new Date();
	}

	@PreUpdate
	public void onUpdate() {
		this.modifyDate = new Date();
	}
}